package UseStream;

import javax.xml.namespace.QName;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamPractice {
    public static void main(String[] args) {
        // practice01();
        //practice02();
        practice03();
    }
    // 过滤奇偶数
    public static void practice01(){
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,1,2,3,4,5,6,7,8,9,10);
        List<Integer> list1 = list.stream().filter(s -> s % 2 == 0).collect(Collectors.toList());
        System.out.println(list1);
    }

    public static void practice02(){
        // xiaoping,25 xiaomei,26 xiaotong 27 保留年龄大于26的，并保存到集合中，名字为键，年龄为值
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"小平,25","小美,26","小丑,27");
        Map<String, Integer> collect = list.stream().filter(s -> {
            String age = s.split(",")[1];
            return Integer.parseInt(age) >= 26;
        }).collect(Collectors.toMap(s -> {
            String name = s.split(",")[0];
            return name;
        }, s -> {
            String age = s.split(",")[1];
            return Integer.parseInt(age);
        }));
        System.out.println(collect);
    }

    public static void practice03(){
        ArrayList<String> man = new ArrayList<>();
        Collections.addAll(man,"蔡坤坤,24","叶小美,23","刘不甜,24","吴签,24",
                "谷嘉,24","肖梁梁,24");
        ArrayList<String> woman = new ArrayList<>();
        Collections.addAll(woman,"赵一枚,35","杨雕,36","高丽丽,43","刘小,35",
                "张大拿,31","杨大咪,33");
        Stream<String> manStream = man.stream().filter(s -> s.split(",")[0].length() == 3).limit(2);
        Stream<String> womanStream = woman.stream().skip(1).filter(s -> s.startsWith("杨"));
        List<Actor> a = Stream.concat(manStream, womanStream).map(s -> {
            String name = s.split(",")[0];
            int age = Integer.parseInt(s.split(",")[1]);
            return new Actor(name, age);
        }).collect(Collectors.toList());
        System.out.println(a);

    }
}

class Actor{
    private String name;
    private int age;

    public Actor() {
    }

    public Actor(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Actor{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
